<?php
    $br = '<br />';
    
    //Get data from request
    $path = $_POST['path'];
    $col = $_POST['col'];
    
    //Set up response
    $response = "";
    
    //Check if is a dir
    if (is_dir($path)) {
        //Get dir handler
        if ($dir_handler = opendir($path)) {
            //Loop for files and directories
            while (($file = readdir($dir_handler)) != false) {
                //Omit '.' files
                if(substr($file, 0, 1) != '.') {
                    //File
                    if(filetype($path . $file) == 'file') {
                        $response .= "<div class='f_entry' type='file' path='$path$file' col='$col'>";
                        $response .= img_tag('file')."<div class='f_name'>$file</div> $br</div>";
                    //Dir
                    } else {
                        $response .= "<div class='f_entry' type='folder' path='$path$file/' col='$col'>";
                        $response .= img_tag('folder')."<div class='f_name'>$file</div>";
                        $response .= img_tag('arrow_right')."$br</div>";
                    }
                }
            }
            closedir($dir_handler);
        }
    }
    
    echo $response;
    
    
    function img_tag($img_name) {
        return "<img class='$img_name' src='img/$img_name.png' alt='$img_name' \>";
    }
?>
